﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using DAO;
using DTO;
using BUS;
namespace QL_xxx
{
	/// <summary>
	/// Interaction logic for QLQuyDinh.xaml
	/// </summary>
	public partial class QLQuyDinh : UserControl
	{
        private List<QuyDinhDTO> DSQuyDinh = null;
        private QuyDinhDTO quyDinhSelected = null;
		public QLQuyDinh()
		{
			this.InitializeComponent();
		}

        private void LoadGrid(List<QuyDinhDTO> listQuyDinh)
        {
            if (listQuyDinh != null && listQuyDinh.Count > 0)
            {
                GridQuyDinh.ItemsSource = listQuyDinh;
                GridQuyDinh.Columns[1].Header = "Tên Quy Định";
                GridQuyDinh.Columns[2].Header = "Mô Tả";

                GridQuyDinh.Columns[1].Width = GridQuyDinh.ActualWidth / 2;
                GridQuyDinh.Columns[2].Width = GridQuyDinh.ActualWidth / 2;

                GridQuyDinh.Columns[0].Visibility = Visibility.Hidden;


            }
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                GridQuyDinh.Columns[1].Width = GridQuyDinh.ActualWidth / 3;
                GridQuyDinh.Columns[2].Width = GridQuyDinh.ActualWidth / 3;
     
            }
            catch
            {
            }
        }

        private void btnSua_Click(object sender, RoutedEventArgs e)
        {
            SuaQuyDinh suaQD = new SuaQuyDinh(quyDinhSelected.MaQuyDinh, quyDinhSelected.TenQuyDinh, quyDinhSelected.MoTa);
            suaQD.onSuaQuyDinhThanhCong += new OnSuaQuyDinhThanhCong(suaQD_onSuaQuyDinhThanhCong);
            suaQD.ShowDialog();
        }
        void suaQD_onSuaQuyDinhThanhCong()
        {
            DSQuyDinh = QuyDinhBUS.LayDSQuyDinh();
            LoadGrid(DSQuyDinh);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DSQuyDinh = QuyDinhBUS.LayDSQuyDinh();
            LoadGrid(DSQuyDinh);
            btnSua.IsEnabled = false;
        }

        private void GridQuyDinh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            quyDinhSelected = (QuyDinhDTO)GridQuyDinh.SelectedItem;
            if (quyDinhSelected != null)
            {
                btnSua.IsEnabled = true;
            }
            else
            {
                btnSua.IsEnabled = false;
            }
        }
     


	}
}